package tuanda.soict.firstsun.AbilityPackage;

import java.util.ArrayList;

import tuanda.soict.firstsun.EffectPackage.Poison_Effect;
import tuanda.soict.firstsun.LogicPackage.Army;
import tuanda.soict.firstsun.LogicPackage.BattleMap;
import tuanda.soict.firstsun.LogicPackage.Coordinate;
import tuanda.soict.firstsun.LogicPackage.Scenario;

/**
 * @author tuanda_soict_hut
 * @version 0.0.1
 * @since 9-9-2012
 */
public class Poison_Ability extends Ability {

	public Poison_Ability(int id, String name, int range, int area,
			int affect) {
		super(id, name, range, area, affect);
	}

	@Override
	public void mainAct(Army army, Scenario scenario) {
		int level = getAbilityLevel();
		Coordinate current = army.getCoordinate();
		int range = this.getAbilityRange();
		Coordinate targetCoor = this.getTarget();
		BattleMap map = scenario.getBattleMap();
		if (current.getDistance(targetCoor) <= range) {
			ArrayList<Coordinate> affectedArea = calculateAffectedArea(army,
					scenario);
			for (Coordinate coor : affectedArea) {
				Army arm = map
						.getArmy(coor.getLocationX(), coor.getLocationY());
				if (arm != null) {
					if (scenario.isInSameSide(arm.getGroupID(),
							army.getGroupID()) == false) {
						Poison_Effect poison_Effect = new Poison_Effect(
								this.getID(), this.getName(), 4, 10, scenario);
						arm.addEffect(poison_Effect);
						army.setExperience(army.getExperience() + (10 + level)
								* arm.getRank(), armyManager);
						army.setActionLeft(false);
					}
				}
			}
		}
	}

	@Override
	public void calculation(Army army, Scenario scenario) {
		Coordinate targetCoor = this.getTarget();
		BattleMap map = scenario.getBattleMap();
		Army arm = map.getArmy(targetCoor.getLocationX(),
				targetCoor.getLocationY());
		if (arm != null) {
			if (scenario.isInSameSide(arm.getGroupID(), army.getGroupID()) == false) {
				this.setAbilityPoint((5 + getAbilityLevel()) * arm.getRank());
			}
		}
	}

	@Override
	public ArrayList<Coordinate> calculateAffectedArea(Army army,
			Scenario scenario) {
		ArrayList<Coordinate> temp = new ArrayList<Coordinate>();
		Coordinate target = this.getTarget();
		if (scenario.getBattleMap().isValidCoordinate(target)) {
			temp.add(target);
		}
		return temp;
	}
}
